package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.category.pojo.Category;
import com.hmall.common.PageBean;
import com.hmall.item.mapper.CategoryMapper;
import com.hmall.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        category.setStatus(2);
        categoryMapper.insert(category);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
    }

    @Override
    public PageBean<Category> list(Integer pageNum, Integer pageSize) {
        //准备两个参数
        //参数1：分页对象Page  参数2：查询条件
        Page<Category> p = new Page<>(pageNum, pageSize);
        QueryWrapper<Category> wrapper = Wrappers.emptyWrapper();

        //调用mapper分页查询
        categoryMapper.selectPage(p, wrapper);

        return new PageBean<>(p.getTotal(), p.getRecords());
    }
}
